package com.isccp.efi.util;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.isccp.efi.dto.TDpcaEfi074;
import com.isccp.efi.dto.TDpcaEfi078;
import com.iss.efi.service.EfiWsService;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
public class GetWsDataUtil {


    private String efiWsUr="http://localhost:86/efiWsService";


    @Value("${encryption.enable}")
    private Boolean encryptionEnable;

    public   List<TDpcaEfi078> getYhl(String vendor) {
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("serviceName","yhlGetService");
        JSONObject data=new JSONObject();
        data.put("VENDOR",vendor);
        data.put("SIZE",100);
        jsonObject.put("data",data);
        String res=dataRecService(jsonObject.toJSONString());
        return JSON.parseArray(res, TDpcaEfi078.class);
    }

    public   JSONObject sendBL(JSONObject data) {
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("serviceName","blReceiveService");
        jsonObject.put("data",data);
        String res=dataRecService(jsonObject.toJSONString());
        return JSON.parseObject(res);
    }





    public   String confirmYhl(String vendor,String ids) {
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("serviceName","yhlConfirmService");
        JSONObject data=new JSONObject();
        data.put("VENDOR",vendor);
        data.put("IDS",ids);
        jsonObject.put("data",data);
        String res=dataRecService(jsonObject.toJSONString());
        return res;
    }



    public   String dataRecService(String params) {
        String paramsData=params;

        try {
            if(encryptionEnable){
                paramsData=SymmetricEncryptionUtil.encrypt(params);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
        factoryBean.setClientFactoryBean(new ClientFactoryBeanImpl());
        factoryBean.setAddress(efiWsUr);
        factoryBean.setServiceClass(EfiWsService.class);
        Object obj = factoryBean.create();
        EfiWsService efiWsService = (EfiWsService) obj;
        String data=efiWsService.efiIssDataOperation(paramsData);

        if(encryptionEnable){
            try {
                data=SymmetricEncryptionUtil.decrypt(data);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return data;
    }

    public static void main(String[] args) throws IOException {

        System.out.println( new GetWsDataUtil().getYhl("M810290000"));

    }


}
